package  Classes.LevelManagement.Levels
{
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Classes.LevelManagement.Level_Base;
	import flash.display.MovieClip;
	import Classes.Main
	import Classes.Utils.PixelPerfectCollisionDetection;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.sampler.NewObjectSample;
	import flash.ui.Mouse;

	/**
	 * ...
	 * @author Rodrigo Forti & Rafael Bringel
	 */
	public class Level1 extends Level_Base
	{			
		var world:b2World;
		var world_scale:int;
		
		public function Level1() 
		{
			
		}
		
		public override function start():void
		{	
			Mouse.show();
			
			world = new b2World(new b2Vec2(50, 50), true);
			world_scale = 30;
			
			draw_box(400, 600, 800, 100, false);
			draw_box(400, 0, 800, 100, false);
			draw_box(0, 300, 100, 600, false);
			draw_box(800, 300, 100, 600, false);
			
			draw_box(250,100,100,100,true);
			draw_circle(400,350,50,false);
			draw_circle(400, 100, 50, true);
		
			
			debug_draw();
		}
		
		
		public function draw_circle(px,py,r,d):void {
			var my_body:b2BodyDef= new b2BodyDef();
			my_body.position.Set(px/world_scale, py/world_scale);
			if (d) 
			{
				my_body.type=b2Body.b2_dynamicBody;
			}
			var my_circle:b2CircleShape=new b2CircleShape(r/world_scale);
			var my_fixture:b2FixtureDef = new b2FixtureDef();
			my_fixture.shape=my_circle;
			var world_body:b2Body=world.CreateBody(my_body);
			world_body.CreateFixture(my_fixture);
		}
		
		public function draw_box(px,py,w,h,d):void {
			var my_body:b2BodyDef= new b2BodyDef();
			my_body.position.Set(px/world_scale, py/world_scale);
			if (d) {
				my_body.type=b2Body.b2_dynamicBody;
			}
			var my_box:b2PolygonShape = new b2PolygonShape();
			my_box.SetAsBox(w/2/world_scale, h/2/world_scale);
			var my_fixture:b2FixtureDef = new b2FixtureDef();
			my_fixture.shape=my_box;
			var world_body:b2Body=world.CreateBody(my_body);
			world_body.CreateFixture(my_fixture);
		}
		
		public function debug_draw():void 
		{
			var debug_draw:b2DebugDraw = new b2DebugDraw();
			var debug_sprite:Sprite = new Sprite();
			Main.getInstance().MC_tela.addChild(debug_sprite);
			debug_draw.SetSprite(debug_sprite);
			debug_draw.SetDrawScale(world_scale);
			debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
			world.SetDebugDraw(debug_draw);
		}

		public override function update():void
		{
			world.Step(1/60,10,10);
			world.ClearForces();
			world.DrawDebugData();

			
			var angleRadian:Number = Math.atan2((300 - mouseY), (400 - mouseX));
            var angleDegree:Number = angleRadian * 180 / Math.PI;

            if (angleDegree < 0)
            {
                angleDegree = angleDegree + 360;
            }

			angleDegree -= 180;

			var angulo:Number = angleDegree;

			
			trace(Math.cos(((angulo) * Math.PI) / 180), Math.sin(((angulo) * Math.PI) / 180));
			world.SetGravity(new b2Vec2(Math.cos(((angulo) * Math.PI) / 180) * 5, Math.sin(((angulo) * Math.PI) / 180) * 5));
			
			//trace(((300 - mouseY) / 600) * 10, ((400 - mouseX) / 800) * 10);
			//world.SetGravity(new b2Vec2((300 - mouseY) / 600, (400 - mouseX) / 800));
		}
		
	}

}